package com.example.activities;

import BDD.DataBaseHelper;
import BDD.Queries;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
import com.example.R;
import com.example.adapters.ImageAdapter;

import java.io.File;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainGallery extends Activity {

    private Uri imageUri;
    private static int id = 0;
    private Cursor cursor;
    private int columnIndex;
    public static DataBaseHelper dataBaseHelper;
    public static SQLiteDatabase sqLiteDatabase;
    private int touchIndex;
    private GridView gridview;
    private static final int TAKE_PHOTO_CODE = 1;



    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maingallerylayout);

        dataBaseHelper = new DataBaseHelper(this);
        sqLiteDatabase = dataBaseHelper.getWritableDatabase();

        gridview = (GridView) findViewById(R.id.mainGallery);

        gridview.setAdapter(new ImageAdapter(this));
        registerForContextMenu((GridView) gridview);
        
        gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                touchIndex = i;
                Intent intent =  new Intent(MainGallery.this, ViewPhoto.class);
                intent.putExtra("position", touchIndex);
                startActivity(intent);
            }
        });


        gridview.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
                touchIndex = position;
                parent.showContextMenu();

                return true;
            }
        });
    }

    @Override

    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) {

        super.onCreateContextMenu(menu, v, menuInfo);

        MenuInflater inflater = getMenuInflater();

        inflater.inflate(R.menu.contextuel, menu);

    }
    public boolean onContextItemSelected(MenuItem item) {



        switch (item.getItemId()) {


            case R.id.itemEdit:

                Intent intent = new Intent(MainGallery.this, EditPhoto.class);
                intent.putExtra("position", touchIndex);
                startActivity(intent);

                return true;



            case R.id.itemDelete:

                File myDirectory = new File(Environment.getExternalStorageDirectory() + "/AppImages/");
                File[] photos = myDirectory.listFiles();
                Queries.deletePicture(photos[touchIndex].getAbsolutePath());
                boolean deleted = photos[touchIndex].delete();
                gridview.setAdapter(new ImageAdapter(this));

                return true;

        }

        return super.onContextItemSelected(item);

    }

    public void takePicture(View view) {
        String sdf;
        sdf = DateFormat.getDateInstance().format(new Date());
        sdf =  new SimpleDateFormat("dd_MM_yyyy").format(new Date());
        sdf += "("+id+")";

        String fileName ="IMG"+ sdf + ".jpg";
        id++;
        File myDirectory = new File(Environment.getExternalStorageDirectory() + "/AppImages/");

        myDirectory.mkdirs();

        File file = new File(myDirectory, fileName);


        imageUri = Uri.fromFile(file);

        Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);

        startActivityForResult(intent,TAKE_PHOTO_CODE);



    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch(requestCode){
                case TAKE_PHOTO_CODE:
                    // System.out.println("*****************************************  ok *********************");
                    Intent intent = new Intent(MainGallery.this,MainGallery.class);
            }
        }
    }

}
